Introduction to GitHub and Version Control


👤Ondřej Mottl

24.04.2025

Mendel University

🙋Hi! I am Ondřej Mottl


Assistant Professor at Charles University

Head of 🧑‍💻Laboratory of Quantitative Ecology


Preparing materials

🕵️Who is here?


Fast & Furious


This presentation



😸Code on GitHub: OndrejMottl/VersionControl_Mendel_Apr2025

🖼️Slides: bit.ly/mottl_prez_20250424


Evolution

Open Science

A better view

The Journey

Version control

Ring a bell?

What is Version Control? 🤔

It is all about keeping track of changes 📓✍️

Exercise

03:00

Do you recognize some of these questions?

  • It broke … hopefully I have a working version somewhere?
  • Can you please send me the latest version?
  • Which version are you using?
  • I am sure it used to work. When did it change?
  • My laptop is gone. Is my data now gone?

How do you keep track of changes?

Fundamentals

Project self-containment




Each “paper” is a single project.

Project structure - RStudio Projects

R-studio is using Projects already by default


Git

  • local software
  • keep track of changes of files


GitHub

  • host server
  • store (git) the data
  • project management, collaboration, publishing

Git/GitHub setup AKA “git hell”

Follow instructions in Version Control - git hell (a separate presentation).

Getting all the necessary software installed, configured, and playing nicely together is honestly half the battle … Brace yourself for some pain

Weapon of choice (GUI)

You will still need to know the basics of shell.

GUI = Graphical User Interface

I will be showing you how to use:

Note on {usethis} package

The {usethis} package is a collection of functions that help various tasks in R.

Install the package by running:

install.packages("usethis")

call a function by running:

usethis::function_name()

Basic vocabulary

  • R script is a record of code.
  • R Project is self contained project/study/paper containg R scripts, data, figures, etc.
  • Every such project is called repository (ie a repo)
  • Your local repository is called local
  • Your online repository, is called remote




Note on practital exercises

Git init (project first)

Activate git for a repo

  1. For existing project
usethis::use_git()
  1. Create new project with git tracking (either via RStudio GUI or via {usethis} package):
usethis::create_project("<DIRECTORY>")
# switch to the new project
usethis::use_git()

Create new project with git tracking

For existing project

git init

Create new project with git tracking

git init <DIRECTORY>

Git integration is automatic in Source control panel

Practical Exercise

  • Make a new project with Git tracking

05:00

A record of a change

a commit

A commit is a record of a change

If you create or edit a file in your repository and save the changes, you need to record your change via a commit

Chess analogy?

Chess move diary:

  • Bc4 (Bishop to c4)
  • Nf3 (Knight to f3)
  • Qc7 (Queen to c7)

a commit

Pawn to d4

Edit line 32 of file A

a commit



3 states of a file


Staging changes

Make a change to a file and save it. Now stage the change:

  • two yellow ?? indicates adding a file
  • a blue M indicates edit a file that has already been committed
  • a red D indicates deleting a file

  • The red icon indicates removed files.
  • The yellow icon indicates modified files.
  • The green icon indicates added files.
git add <FILE>

Practical Exercise

  1. Make changes to (a) file(s)
  2. Make a new file
  3. Stage and Unstage the changes

05:00

a first commit

Commit (record) staged changes:

git commit -am "commit message"

Review history

$ git log --stat
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 16:40:33 2008 -0700

    Remove unnecessary test

 lib/simplegit.rb | 5 -----
 1 file changed, 5 deletions(-)

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 10:31:28 2008 -0700

    Initial commit

 README           |  6 ++++++
 Rakefile         | 23 +++++++++++++++++++++++
 lib/simplegit.rb | 25 +++++++++++++++++++++++++
 3 files changed, 54 insertions(+)

Dissecting a commit

SHA - unique identifier

Author - who has done this?

Date - when was this done?

Message - description of what has been done

Stats - what has changed?

Practical Exercise

  1. commit some changes
  2. review history

05:00

Commit message

Commits are quick and cheap. Therefore:

  1. commit often (!)
  2. provide useful commit messages.

Commit history

Remote

remote

Update remote - PUSH

Now we need to sync chnages with the remote using PUSH

Add a remote to existing local repo (only once):

usethis::use_github(protocol  = "https")

Push local to remote (GitHub):

Add a remote to existing local repo (only once):

Push local to remote (GitHub):

Add a remote to existing local repo (only once):

git remote add origin https://github.com/<OWNER>/<REPO>

Push local to remote (GitHub):

git push

Add a remote to existing local repo (only once):

Push local to remote (GitHub):

Practical Exercise

  1. Publish repo to GitHub
  2. make new commit(s)
  3. Push changes to remote

05:00

update local- PULL

update local- PULL

update local- PULL

Now we need to sync chnages from the remote to local the using PULL

Pull from remote (GitHub) to local

Pull from remote (GitHub) to local

Pull from remote (GitHub) to local:

git pull

Pull from remote (GitHub) to local

GitHub intermezzo

A GitHub repo

GitHub creating a repo (repo first)

GitHub creating a repo (repo first)

GitHub creating a repo (repo first)

GitHub creating a repo (repo first)

GitHub creating a repo (repo first)

README - description of the project

.gitignore - list of files ignored by GitHub (more about it later)

license - tell other what they can do wit your code

GitHub creating a repo (repo first)

Practical Exercise

Create a new repo on GitHub

05:00

Git Clone (repo first)

Git clone (repo first)

Copy (download) from remote repo to local machine

Example of online repo: OndrejMottl/VersionControl-playground

Clone repo using new project in RStudio GUI or via {usethis} package:

usethis::create_from_github(
  repo_spec = "https://github.com/<OWNER>/<REPO>.git",
  destdir = "<DIRECTORY>",
  fork = FALSE
  )

git clone https://github.com/<OWNER>/<REPO>.git <DIRECTORY>

Open Command Palette (Ctrl+Shift+p)

Paste in URL: "https://github.com/<OWNER>/<REPO>.git"

Practical Exercise

clone a repo (e.g. any repo from OndrejMottl)

05:00

Merge conflict

Merge conflict 💩💩💩

A merge conflict can occur when you are changing the same line in one file differently.

Merge conflict 💩💩💩


To https://github.com/picardis/myrepo.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/picardis/myrepo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

a good strategy to avoid such conflicts:

  • Commit often
  • Work in small steps
  • Push and pull regularly
  • Organize your code in small modules (scripts)


Merge conflicts cannot always be avoided (but can be mitigated by branches; later).

Merge conflict 💩💩💩

If you have questions, please
<<<<< HEAD
open an issue
=======
ask your question in IRC.
>>>>> branch-a

Delete the unwanted text (including the decorations)

If you have questions, please
ask your question in IRC.

Then save the file, stage, and commit again

Ups! I have made a mistake 😮

How to undo last commit?

Variant A: I commited but NOT pushed yet.

RStudio has a range of possibilities to work with Git and GitHub as shown in this tutorial. The Terminal (NOT console) has more commands and options and will be handy for trouble shooting.

git reset --soft HEAD@{1}

git reset --soft HEAD@{1}

Open Command Palette (Ctrl+Shift+p)

Write Git: Undo Last Commit

Ups! I have made a mistake 😮

How to undo last commit?

Variant B: I commited but AND pushed already.

We need the Terminal (NOT console) again.

Copy the SHA of the last commit

git reset --hard <SHA>

Right-click on the commit you would like to undo to and select Revert a commit.

Copy the SHA of the last commit

git reset --hard <SHA>

In the Source control panel -> COMMITS section -> Right-click on the commit you want to revert to -> Select the Reset Current Branch to Previous Commit

Branches

Branches

Branches

Make a branch

Copy the SHA of the last commit

git branch <BRANCH-NAME>

Switching between branches

The default branch is called main or master

Swithching between branches is sometimes called (checkout)

‼️ Make sure that you have all changes commited before switching ‼️

Copy the SHA of the last commit

git checkout  <BRANCH-NAME>

Practical Exercise

  1. Make a branch and switch
  2. commit changes
  3. push to remote

05:00

Merging branches

Merging branches

Pull Request (PR)

Request to merge a branch

Pull Request - create

After you push new branch, you should have a green button Compare & pull request on GitHub

Pull Request - components

Pull Request - Overview

Now you can more commits on GitHub, (add Comment to start discussion), or merge the branch.

Practical Exercise

  1. Create a PR

25:00

Note on Markdown

You can use Markdown on GitHub in the description and comments

More details on Github Docs

Pull Request - review

A tool to review on GitHub suggested changes

Collaboration

Pull Request - review

On someone else’s PR on GitHub, you can comment on individual lines or whole files

Pull Request - review

Merging branches

Merge conflict with branch 💩

Merge conflict with branches is much more pleasant😎

Merge conflict with branch 💩

Edit the file as needed

Merge conflict with branch 💩

Commit the changes

Practical Exercise

Merge a branch

05:00

Delete branch

We can delete branch directly on GitHub after merging

Delete branch

We can also delete branch before merging

We need the Terminal (NOT console) again.

To delete a local branch

git branch -d <BRANCH-NAME>

To delete a remote on GitHub branch

git push origin --delete <BRANCH-NAME>

To delete a local branch

git branch -d <BRANCH-NAME>

To delete a remote on GitHub branch

git push origin --delete <BRANCH-NAME>

Open Command Palette (Ctrl+Shift+p)

Select Git: Delete branch …

Practical Exercise

Delete a branch

05:00

PR workflow with {usethis}

This is just a teaser

GitHub has a lot of features and tools to make your life easier:

  • Project management
  • Task management
  • Collaboration
  • Dissimination
  • Automation

Maybe next time?




Introduction to GitHub and Version Control

27.3.2025